import { createWebHistory, createRouter } from 'vue-router'
import Layout from '@/layout'
import { TrendCharts } from '@element-plus/icons-vue'

// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/sociallogin',
    component: () => import('@/views/socialLogin'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '/error',
    component: () => import('@/views/error/Error'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/workbench',
    children: [
      {
        path: 'workbench',
        component: () => import('@/pages/workbench/Workbench.vue'),
        name: 'workbench',
        meta: {
          title: "首页工作台",
          icon: "dashboard"
        }
      }
    ]
  },
  {
    path: "/mall",
    component: Layout,
    redirect: "/mall/home",
    meta: { title: "商城", icon: "ShoppingCart" },
    children: [
      {
        path: "home",
        name: "MallHome",
        component: () => import("@/pages/mall/MallCompanyList.vue"),
        meta: { title: "商城首页" }
      },
      {
        path: "product/:id",
        name: "ProductDetail",
        component: () => import("@/pages/mall/ProductDetail.vue"),
        meta: { title: "产品详情", hidden: true }
      },
      {
        path: "trade",
        name: "TradeCenter",
        component: () => import("@/pages/mall/TradeCenter.vue"),
        meta: { title: "交易中心" }
      },
      {
        path: "user",
        name: "UserCenter",
        component: () => import("@/pages/mall/UserCenter.vue"),
        meta: { title: "个人中心" }
      },
      {
        path: "data",
        name: "DataCenter",
        component: () => import("@/pages/mall/DataCenter.vue"),
        meta: { title: "数据中心" }
      },
      {
        path: "service",
        name: "ServiceCenter",
        component: () => import("@/pages/mall/ServiceCenter.vue"),
        meta: { title: "服务中心" }
      }
    ]
  },
  {
    path: '/business-center',
    name: 'BusinessCenter',
    component: Layout,
    meta: { title: '买家与卖家业务中心', icon: 'BusinessCenter' },
    children: [
      {
        path: 'after-sale',
        name: 'AfterSalePage',
        component: () => import('@/pages/business-center/AfterSalePage.vue'),
        meta: { title: '售后管理' }
      },
      {
        path: 'invitation',
        name: 'InvitationPage',
        component: () => import('@/pages/business-center/InvitationPage.vue'),
        meta: { title: '邀请管理与竞价流程' }
      },
      {
        path: 'deposit',
        name: 'DepositPage',
        component: () => import('@/pages/business-center/DepositPage.vue'),
        meta: { title: '保证金管理' }
      },
      {
        path: 'sourcing',
        name: 'SourcingPage',
        component: () => import('@/pages/business-center/SourcingPage.vue'),
        meta: { title: '产品寻源发起与管理' }
      },
      {
        path: 'seller',
        name: 'SellerPage',
        component: () => import('@/pages/business-center/SellerPage.vue'),
        meta: { title: '卖家中心' }
      }
    ]
  }
  ,
  {
    path: '/inventory',
    name: 'InventoryManage',
    component: Layout,
    meta: { title: '仓储库存管理', icon: 'InventoryManage' },
    children: [
      {
        path: 'stock',
        name: 'StockPage',
        component: () => import('@/pages/inventory/StockPage.vue'),
        meta: { title: '库存现有量' }
      },
      {
        path: 'inventory',
        name: 'InventoryPage',
        component: () => import('@/pages/inventory/InventoryPage.vue'),
        meta: { title: '库存盘点' }
      },
      {
        path: 'outbound',
        name: 'OutboundPage',
        component: () => import('@/pages/inventory/OutboundPage.vue'),
        meta: { title: '销售出库' }
      },
      {
        path: 'warehouse',
        name: 'WarehousePage',
        component: () => import('@/pages/inventory/WarehousePage.vue'),
        meta: { title: '仓库设置' }
      }
    ]
  },
  {
    path: '/base-config',
    name: 'BaseConfig',
    component: Layout,
    meta: { title: '基础信息', icon: 'BaseConfig' },
    children: [
      {
        path: 'company',
        name: 'CompanyPage',
        component: () => import('@/pages/base-config/CompanyPage.vue'),
        meta: { title: '公司资料' }
      },
      {
        path: 'basic-data',
        name: 'BasicDataPage',
        component: () => import('@/pages/base-config/BasicDataPage.vue'),
        meta: { title: '基础资料' }
      }
    ]
  },
  {
    path: '/business',
    name: 'BusinessManage',
    component: Layout,
    meta: { title: '业务管理', icon: 'BusinessManage' },
    children: [
      {
        path: 'sales',
        name: 'SalesPage',
        component: () => import('@/pages/business/SalesPage.vue'),
        meta: { title: '销售' }
      },
      {
        path: 'purchase',
        name: 'PurchasePage',
        component: () => import('@/pages/business/PurchasePage.vue'),
        meta: { title: '采购' }
      },
      {
        path: 'outsourcing',
        name: 'OutsourcingPage',
        component: () => import('@/pages/business/OutsourcingPage.vue'),
        meta: { title: '外协' }
      }
    ]
  },
  {
    path: '/system',
    name: 'SystemManage',
    component: Layout,
    meta: { title: '系统运营与设置', icon: 'SystemManage' },
    children: [
      {
        path: 'operation',
        name: 'OperationPage',
        component: () => import('@/pages/system/OperationPage.vue'),
        meta: { title: '运营端' }
      },
      {
        path: 'settings',
        name: 'SettingsPage',
        component: () => import('@/pages/system/SettingsPage.vue'),
        meta: { title: '设置中心' }
      }
    ]
  }




]

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_ROUTER_PREFIX),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

export default router
